package china.accountinghelper.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import china.wc.tallyhelper.R;
import china.wc.tallyhelper.adapter.AdapterExpandableListViewBookDetail;
import china.wc.tallyhelper.business.BusinessExpense;
import china.wc.tallyhelper.controls.SlideMenu;
import china.wc.tallyhelper.controls.SlideMenuItem;
import china.wc.tallyhelper.model.ModelBook;
import china.wc.tallyhelper.model.ModelExpense;

public class ActivityBookDetail extends ActivityFrame implements OnClickListener
{
    public static final String             SELECTED_MODEL_OBJECT        = "ModelExpense";
    public static final int                REQUEST_CODE_EDIT_EXPENSE      = 1;
    public static final int                RESULT_ERR                  = RESULT_FIRST_USER + 1;
    public static final int DIALOG_CONFIRM_EXPENSE_DELETE = 1;
    
    private BusinessExpense mBusinessExpense;
    
    private AdapterExpandableListViewBookDetail mAdapterExpandableListViewBookDetail;
    
    private ExpandableListView mFramebodyBookDetail_ExpandableListView_BookDetail;
    
    private ModelBook mHandlingModelBook;
    private ModelExpense               mHandlingModelExpense;

    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // 载入主页面Layout文件
        loadFramebody(R.layout.framebody_book_detail);
        // 设置标题
        setTopTitle(R.string.title_book_detail);

        initFields();
        initViews();
        initListeners();
        bindData();
        register();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (mAdapterExpandableListViewBookDetail!=null) {
            mAdapterExpandableListViewBookDetail.notifyDataSetChanged();
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {
        ExpandableListView.ExpandableListContextMenuInfo _MenuInfo = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
        int _PositionType = ExpandableListView.getPackedPositionType(_MenuInfo.packedPosition);
        
        if (_PositionType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            int _GroupPosition = ExpandableListView.getPackedPositionGroup(_MenuInfo.packedPosition); 
            mHandlingModelExpense = mAdapterExpandableListViewBookDetail.getGroup(_GroupPosition);
            inflateMenu(R.menu.contextmenu_bookdetaillist, menu);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        if (item.getItemId() == R.id.ContextMenu_BookDetailList_EditExpense) {
            Bundle _Bundle = new Bundle();
            _Bundle.putSerializable(SELECTED_MODEL_OBJECT, mHandlingModelExpense);
            openActivityForResult(ActivityExpenseAddAndEdit.class, _Bundle, REQUEST_CODE_EDIT_EXPENSE);
        } else if (item.getItemId() == R.id.ContextMenu_BookDetailList_DeleteExpense) {
            showDialog(DIALOG_CONFIRM_EXPENSE_DELETE, null);
        }
        
        
        return false;
    }

    @Override
    protected Dialog onCreateDialog(int id, Bundle args)
    {
        AlertDialog.Builder _AlertDialogBuilder = new AlertDialog.Builder(this);

        // 删除警告对话框
        if (id == DIALOG_CONFIRM_EXPENSE_DELETE) {
            _AlertDialogBuilder.setTitle(R.string.title_expense_delete)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage(getString(R.string.message_expense_check_delete))
                    .setPositiveButton(R.string.ok, this)
                    .setNegativeButton(R.string.cancel, this);
        }

        return _AlertDialogBuilder.create();
    }

    @Override
    public void onClick(DialogInterface dialog, int which)
    {
        if (getShowingDialogID() == DIALOG_CONFIRM_EXPENSE_DELETE) {
            if (which == Dialog.BUTTON_POSITIVE) {
                mBusinessExpense.deleteModel(mHandlingModelExpense);
                mAdapterExpandableListViewBookDetail.notifyDataSetChanged();
            } else if (which == Dialog.BUTTON_NEGATIVE) {
                dialog.dismiss();
            }
        }
        
        
    }

    @Override
    public void onSlideMenuItemClick(SlideMenu pSlideMenu, SlideMenuItem pSlideMenuItem, AdapterView<?> pParent, View pView, int pPosition, long pId)
    {
    }

    @Override
    protected void initFields()
    {
        mBusinessExpense = new BusinessExpense();
        mHandlingModelBook = (ModelBook) getIntent().getSerializableExtra(ActivityBook.SELECTED_MODEL_OBJECT);
        mAdapterExpandableListViewBookDetail = new AdapterExpandableListViewBookDetail(this, mHandlingModelBook);
        
    }

    @Override
    protected void initViews()
    {
        mFramebodyBookDetail_ExpandableListView_BookDetail = (ExpandableListView) findViewById(R.id.FramebodyBookDetail_ExpandableListView_BookDetail);
        mFramebodyBookDetail_ExpandableListView_BookDetail.setEmptyView(findViewById(R.id.FramebodyBookDetail_LinearLayout_Empty));
    }

    @Override
    protected void initListeners()
    {
    }

    @Override
    protected void bindData()
    {
        mFramebodyBookDetail_ExpandableListView_BookDetail.setAdapter(mAdapterExpandableListViewBookDetail);
    }

    @Override
    protected void register()
    {
        registerForContextMenu(mFramebodyBookDetail_ExpandableListView_BookDetail);
    }
    
}
